const app = getApp();
const moment = require('../../utils/moment-with-locales.min.js');
moment.locale('zh-cn');
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    tabCur0Loading: false,
    tabCur0Data: [],
    searchStr: ""
  },
  onLoad() {
    this.doSearch({});
  },
  onShow() {
    var param = {
      type: "tap"
    };
    this.doSearch(param);
  },
  doDetail(e) {
    var id = e.currentTarget.dataset.id;
    var that = this;
    wx.navigateTo({
      url: '/pages/detail/detail?id=' + id
    })
  },
  doSearch: function(e) {
    var searchStr = '';
    if (e.type == 'confirm') {
      searchStr = e.detail.value;
    } else if (e.type == 'tap') {
      searchStr = this.data.searchStr;
    }
    var that = this;
    const param = {
      sessionKey: app.globalData.sessionId,
      searchStr: searchStr
    };
    wx.request({
      url: app.url.getAllListData,
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: param,
      success: function(res) {
        const data = res.data;
        const list = data.data;
        if (data.result == "true") {
          that.formatCountDown(list);
        } else {
          wx.showModal({
            content: data.message,
            showCancel: false,
            success: function(res) {}
          });
          that.setData({
            tabCur0Loading: false
          });
        }
      },
      fail: res => {
        wx.showModal({
          content: "获取列表数据失败！",
          showCancel: false,
          success: function(res) {}
        });
      }
    });
  },
  listenerSearchStr(e) {
    this.setData({
      searchStr: e.detail.value
    });
  },
  formatCountDown(list) {
    for (const obj of list) {
      obj.fromNow = moment(obj.orderTime).fromNow();
      obj.countDown = this.getCountDown(obj.orderTime, obj.delayMinute);
      if (!obj.countDown) {
        obj.overTime = true;
      }
    }
    this.setData({
      tabCur0Loading: false,
      tabCur0Data: list
    });
  },
  getCountDown(dateStr, delayMinute) {
    const diff = moment(dateStr).add(delayMinute, 'minute').diff(moment(), 'seconds');
    if (diff <= 0) {
      return null;
    }
    var diffMinute = parseInt(diff / 60);
    var diffSecond = diff % 60
    var countDownStr = (diffMinute > 0 ? (diffMinute + '分') : '') + diffSecond + '秒';
    return countDownStr;
  }
})